package org.chen.newsapi.common;

import lombok.Data;
import java.util.List;

@Data
public class Page<T> {
    private List<T> content;
    private Integer totalPages;
    private Long totalElements;
    private Integer size;
    private Integer number;
    private Boolean hasNext;

    public Page(List<T> content, Integer totalPages, Long totalElements, Integer size, Integer number, Boolean hasNext) {
        this.content = content;
        this.totalPages = totalPages;
        this.totalElements = totalElements;
        this.size = size;
        this.number = number;
        this.hasNext = hasNext;
    }

    public static <T> Page<T> of(List<T> content, Integer currentPage, Integer pageSize, Long totalElements) {
        Integer totalPages = (int) Math.ceil((double) totalElements / pageSize);
        Boolean hasNext = currentPage < totalPages;
        return new Page<>(content, totalPages, totalElements, pageSize, currentPage, hasNext);
    }
}